//
//  WFDiscoverySearchView.swift
//  WangFangWeiBo
//
//  Created by 王方 on 16/7/12.
//  Copyright © 2016年 wf. All rights reserved.
//

import UIKit


class WFDiscoverySearchView: UIView {

    @IBOutlet weak var searchTF: UITextField!
   
    @IBOutlet weak var cancleBtn: UIButton!

    @IBOutlet weak var rightConstrant: NSLayoutConstraint!
    
    //加载xib
  class  func searchView()->WFDiscoverySearchView{
    
         return NSBundle.mainBundle().loadNibNamed("WFDiscoverySearchView", owner: nil, options: nil).last as! WFDiscoverySearchView
//return UINib(nibName: "WFDiscoverySearchView", bundle: nil).instantiateWithOwner(nil, options: nil).last! as! WFDiscoverySearchView
    }
    //点击取消的时候执行
    @IBAction func cancelAction(sender: UIButton) {
        searchTF.resignFirstResponder()
        rightConstrant.constant = 0
        
        UIView.animateWithDuration(0.25) { () -> Void in
            self.layoutIfNeeded()
            
        }
    }
    //文本框获取焦点的时候

}

extension WFDiscoverySearchView:UITextFieldDelegate{
    func textFieldDidBeginEditing(textField: UITextField) {
        //改变左侧的距离约束
        rightConstrant.constant = cancleBtn.width
        UIView.animateWithDuration(0.5) { () -> Void in
            //这句话至关重要,更新约束
            self.layoutIfNeeded()
        }
    }

}
